package com.etc.exception.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.etc.common.result.ResultMapper;

/**
 * @description
 * @author 黄宇 E-mail: yuanddan2006@126.com
 * @date 创建时间：2016年3月21日 下午2:10:11
 * @version 1.0
 */
public class ExceptionHandler implements HandlerExceptionResolver {
	
	Logger log = LoggerFactory.getLogger(ExceptionHandler.class);

	/**
	 * @description 接收controller的异常，进行错误反馈和打印异常信息
	 *              目前做的是最简单的异常信息打印
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
			Object handler, Exception ex) {
		ModelAndView view = new ModelAndView(new MappingJackson2JsonView());
		
		// 统一打印异常信息
		log.error("",ex);
		
		// 拋出异常后统一返回false的json
		view.addObject(ResultMapper.convertFailure());

		return view;
	}

}
